package com.oyjd.soft;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.oyjd.FileOP;
import com.oyjd.HttpNet;
import com.oyjd.RegexUtil;

public class NetDeal {

	public static String url = "http://apk.hiapk.com";
	public static String url_app_list = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList";
	public static String url_game_list = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList";

	public static String soft_url = "http://static.apk.hiapk.com";

	public static String reg1 = "<span class=\"font14 list_title\"><a href=\"(.*?)\" title=\".*?\">";

	public static String reg_detail_1 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftName\">(.*?)</label>";
	public static String reg_detail_2 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftVersionName\">(.*?)</label>";
	public static String reg_detail_3 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftDeveloper\">(.*?)<";
	public static String reg_detail_4 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftSuitSdk\">([0-9]+(.[0-9]+)?).*?</label>";
	public static String reg_detail_5 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_Description\">(.*?)</label>";
	public static String reg_detail_6 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftUpdateDescription\">(.*?)</label>";
	public static String reg_detail_7 = "<img\\s*width=\"72\"\\s*height=\"72\" src=\"(.*?)\".*?>";
	public static String reg_detail_8 = "<div\\s*class=\"butn_bg butn_3 btnmarg\\s*\">\\s*<a href=\"(.*?)\" class=\"linkbtn d1\".*?>";

	public static String reg_detail_91 = "<div class=\"screenimg\">\\s*<ul>(.*?)</ul>.*?</div>";
	public static String reg_detail_92 = "<img src=\"(.*?)\".*?/>";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

	}

	static {
		HttpNet.setRequestProperty("Referer", url);
	}

	public static List<String> getLink(boolean isGame) throws Exception {
		HttpNet hn = new HttpNet();
		// 解析软件地址
		List<String> all = new ArrayList<String>();
		for (int i = 1; i <= 50; i++) {
			System.out.println("------->page:" + i);
			String page = getList(hn, i, isGame ? url_game_list : url_app_list);
			List<String> tmpList = RegexUtil.getMuti(page, reg1);
			for (String s : tmpList) {
				if (!all.contains(s)) {
					all.add(s);
				}
			}
			Thread.sleep(3000);
		}
		System.out.println("----->共" + all.size() + "个【" + (isGame ? "游戏" : "应用") + "】软件");
		return all;
	}

	/**
	 * 获取列表
	 * 
	 * @param hn
	 * @param page
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String getList(HttpNet hn, int page, String url) throws Exception {
		Map<String, Object> p = new HashMap<String, Object>();
		p.put("curPageIndex", page);
		p.put("sortType", 1);
		p.put("categoryId", 0);
		p.put("totaldownloads", 0);
		p.put("ratings", 0);
		p.put("commentcount", 0);
		p.put("language", 0);
		p.put("platform", 0);
		p.put("currentHash", page + "_1_1_0_0_0_0_0");
		p.put("isInit", false);
		p.put("isPage", true);
		String c = new String(hn.getBytesFromNet(url, p));
		return c;
	}

	public static boolean doOneSoft(SoftVO vo) {
		try {
			HttpNet hn = new HttpNet();
			String c = null;
			try {
				Thread.sleep(2000);
				c = new String(hn.getBytesFromNet(vo.getLink()), "utf-8");
			} catch (Exception e) {
				System.out.println("软件明细的link错误======>" + vo.getLink());
				return false;
			}
			// 解析
			String name = RegexUtil.getSingle(c, reg_detail_1);
			String ver = RegexUtil.getSingle(c, reg_detail_2);
			String author = RegexUtil.getSingle(c, reg_detail_3);
			String apply_comp = RegexUtil.getSingle(c, reg_detail_4);
			String desc = RegexUtil.getSingle(c, reg_detail_5);
			String upDesc = RegexUtil.getSingle(c, reg_detail_6);
			String icon_path = RegexUtil.getSingle(c, reg_detail_7);
			String apk_path = RegexUtil.getSingle(c, reg_detail_8);
			String imgContent = RegexUtil.getSingle(c, reg_detail_91);
			String shot1 = null;
			String shot2 = null;
			String shot3 = null;
			String shot4 = null;
			String shot5 = null;
			if (null != imgContent && !"".equals(imgContent)) {
				List<String> imgs = RegexUtil.getMuti(imgContent, reg_detail_92);
				if (imgs.size() > 0) {
					shot1 = imgs.get(0);
				}
				if (imgs.size() > 1) {
					shot2 = imgs.get(1);
				}
				if (imgs.size() > 2) {
					shot3 = imgs.get(2);
				}
				if (imgs.size() > 3) {
					shot4 = imgs.get(3);
				}
				if (imgs.size() > 4) {
					shot5 = imgs.get(4);
				}
			}

			// 特殊处理
			name = filter(name);
			author = filter(author);
			desc = filter(desc);
			upDesc = filter(upDesc);

			vo.setName(name);
			vo.setVer(ver);
			vo.setAuthor(author);
			vo.setApply_comp(apply_comp);
			vo.setDesc(desc);
			vo.setUpDesc(upDesc);
			vo.setIcon_path(icon_path);
			vo.setApk_path(apk_path);
			vo.setShot1(shot1);
			vo.setShot2(shot2);
			vo.setShot3(shot3);
			vo.setShot4(shot4);
			vo.setShot5(shot5);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static void doDownSoft(SoftVO vo, String outPath) {
		HttpNet.setRequestProperty("Referer", url);
		String t_apkPath = soft_url + vo.getApk_path();
		HttpNet hn = new HttpNet();
		downSou(hn, vo.getIcon_path(), vo.getIcon_path(), "sl_icon", outPath);
		downSou(hn, t_apkPath, t_apkPath + ".apk", "sl_apk", outPath);
		downSou(hn, vo.getShot1(), vo.getShot1(), "sl_shotcut", outPath);
		downSou(hn, vo.getShot2(), vo.getShot2(), "sl_shotcut", outPath);
		downSou(hn, vo.getShot3(), vo.getShot3(), "sl_shotcut", outPath);
		downSou(hn, vo.getShot4(), vo.getShot4(), "sl_shotcut", outPath);
		downSou(hn, vo.getShot5(), vo.getShot5(), "sl_shotcut", outPath);
	}

	/**
	 * 特殊字符处理
	 * 
	 * @param s
	 * @return
	 */
	private static String filter(String s) {
		s = null == s ? "" : s.replaceAll("&nbsp;", " ").replaceAll("</br>", "\r\n").trim();
		s = null == s || "".equals(s.trim()) ? "" : s.trim().replaceAll("\\r\\n", "\\\\r\\\\n").replaceAll("'", "''");
		return s;
	}

	/**
	 * 下载资源
	 * 
	 * @param hn
	 * @param url
	 * @param oldName
	 * @param pre
	 * @return
	 */
	private static boolean downSou(HttpNet hn, String link, String oldName, String pre, String outPath) {
		boolean flag = false;
		if (null == link || "".equals(link)) {
			return flag;
		}
		String newName = FileOP.getNewHashName(oldName, pre);
		File file = new File(outPath + newName);
		if (file.exists() && file.length() == 0) {
			file.delete();
		}
		if (!file.exists()) {
			try {
				Thread.sleep(2000L);
				hn.downFile(link, null, outPath + newName);
				if (!new File(outPath + newName).exists()) {
					System.out.println("无效链接----->" + link);
				} else {
					System.out.println("-------->下载完成：" + link);
					flag = true;
				}
			} catch (Exception e) {
				System.out.println("异常链接----->" + link);
			}
		} else {
			flag = true;
		}
		return flag;
	}
}
